require('../../assets/css/public.less')
require('./register.less')

// 引入验证码
var CaptchaMini = require("captcha-mini")


// 导入请求实例
const request = require('../../lib/request/request.js')
// dom加载完毕
window.onload = () => {
    /*不传值，统一走默认值*/
    let captcha1 = new CaptchaMini({
        fontSize: 60,
    });
    let qwe = '';
    captcha1.draw(document.querySelector('#captcha1'), r => {
        // console.log(r, '验证码1');
        qwe = r
    });
    console.log(qwe);
    function $(sel) {
        return document.querySelector(sel)
    }

    function $a(sel) {
        return document.querySelectorAll(sel)
    }
    // 判断手机号是否正确
    let reg1 = /^1[345678][0-9]{9}$/;

    // 判断手机验证码是否正确
    // let reg2 = /^[0-9]{4}$/;

    // 判断密码是否正确
    let reg3 = /^[0-9]{5,11}$/;

    // 点击立即注册 验证登录与否
    $('.submit').addEventListener('click', function (e) {
        // console.log(11);
        // let msg = ''
        if (!reg1.test($('.phonum').value)) {
            console.log('手机号码');
            $('.error').classList.remove('dpnone')

            // 错误弹窗定时器
            setTimeout(function () {
                $('.error').classList.add('dpnone')
            }, 1000)
            // msg = "手机号格式不对"

            // 验证码
        } else if ($('.kuang1').value !== qwe) {
            console.log('验证码');
            $('.error').classList.remove('dpnone')

            // 错误弹窗定时器
            setTimeout(function () {
                $('.error').classList.add('dpnone')
            }, 1000)

        } else if (!reg3.test($('.pwd').value)) {
            console.log('密码1');
            $('.error').classList.remove('dpnone')

            // 错误弹窗定时器
            setTimeout(function () {
                $('.error').classList.add('dpnone')
            }, 1000)

            // 密码
        } else if ($('.rpwd').value !== $('.pwd').value) {
            console.log('密码2');
            $('.error').classList.remove('dpnone')

            // 错误弹窗定时器
            setTimeout(function () {
                $('.error').classList.add('dpnone')
            }, 1000)
        } /* else if (reg1.test($('.phonum').value) && ($('.kuang1').value !== qwe) && reg3.test($('.pwd').value) && ($('.rpwd').value === $('.pwd').value)) {
            console.log(1);
            $('.succeed').classList.remove('dpnone');
            setTimeout(function () {
                $('.succeed').classList.add('dpnone')
            }, 1000)
            location.href = './login.html'


        } */else {
            console.log('post');
            //发送请求检测 账号是否存在
            request
                .post('/api/user/isExist', {
                    account: $('.phonum').value,
                })
                .then(res => {
                    console.log('res :>> ', res)
                    //解构数据
                    let { errno, message } = res.data
                    //判断
                    if (errno === 0) {
                        //发送请求 注册账号
                        request
                            .post('/api/user/register', {
                                account: $('.phonum').value,
                                password: $('.pwd').value,
                            })
                            .then(res => {
                                //1 解构数据
                                let { data, errno } = res.data
                                //2 判断
                                if (errno === 0) {
                                    //3 处理数据
                                    // localStorage.setItem('userInfo', JSON.stringify(data))
                                    // alert('注册成功')
                                    //跳转到登录页面
                                    window.location.href = './login.html'
                                }
                            })
                            .catch(err => {
                                console.log('err :>> ', err)
                            })
                            .finally(() => {
                                console.log('不管成功or失败都会走 finally')
                            })
                    } else {
                        alert(message)
                    }
                })
        }
    })


}
